package cn.topcodes.tcsf.admin.service.impl;

import cn.topcode.unicorn.utils.DateUtil;
import cn.topcodes.tcsf.admin.scheduling.ScheduleJob;
import cn.topcodes.tcsf.admin.scheduling.ScheduleService;
import com.cronutils.builder.CronBuilder;
import com.cronutils.model.definition.CronDefinitionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import static com.cronutils.model.CronType.QUARTZ;
import static com.cronutils.model.field.expression.FieldExpressionFactory.*;
import static com.cronutils.model.field.expression.FieldExpressionFactory.questionMark;

/**
 * 系统任务
 * @author unicorn
 */
/*@Component
@Lazy(false)*/
public class SysTask {

    @Autowired
    private ScheduleService scheduleService;

    @PostConstruct
    public void init() {
        ScheduleJob scheduleJob = new ScheduleJob();
        scheduleJob.setBeanId("sysTask");
        scheduleJob.setIsConcurrent(true);
        scheduleJob.setJobGroup("系统任务组");
        scheduleJob.setJobName("测试任务");
        scheduleJob.setJobStatus(ScheduleJob.STATUS_PAUSED);
        //  每五秒执行一次
        String cron = CronBuilder.cron(CronDefinitionBuilder.instanceDefinitionFor(QUARTZ))
                .withYear(always())
                .withDoW(questionMark())
                .withMonth(always())
                .withDoM(always())
                .withHour(always())
                .withMinute(always())
                .withSecond(every(on(0),5))
                .instance().asString();
        scheduleJob.setCronExpression(cron);
        scheduleJob.setMethodName("test");

        scheduleService.addJob(scheduleJob);
    }

    public void test() {
        System.out.println(DateUtil.formatCurrentDate() + " 执行测试任务");
    }

}
